<template>
  <div>
    <div class="messageBox">
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12" style="width:400px;">

            <!-- 搜索 -->
            <div class="search">
              <el-input placeholder="请输入内容" v-model="queryInfo.query" @clear="getUserList">
                <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
              </el-input>
            </div>

          </el-col>
          <el-col :span="9" style="width:300px;margin-left:100px;">
            <el-date-picker type="date" placeholder="选择日期" v-model="form.postTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-row>
        <el-table ref="multipleTable" @selection-change="handleSelectionChange" tooltip-effect="dark" :data="tableData2" border style="width: 100%;margin-top:50px;">
          <el-table-column prop="选择" type="selection" width="55">
          </el-table-column>
          <el-table-column prop="feedback" label="反馈人" width="180">
          </el-table-column>
          <el-table-column prop="photo" label="提供图片">
            <template v-slot="{row}" style="width:80px;height:30px ">
              <div class="block">
                <el-carousel height="150px">
                  <el-carousel-item v-for="item in 4" :key="item">
                    <img :src="row.photo" alt="" />
                  </el-carousel-item>
                </el-carousel>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="questType" label="问题类型">
          </el-table-column>
          <el-table-column prop="questDes" label="问题描述">
          </el-table-column>
          <el-table-column prop="phonenum" label="用户联系方式">
          </el-table-column>
          <el-table-column prop="postTime" label="提交时间">
          </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button @click.native.prevent="deleteRow(scope.row)" type="text" size="small">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-checkbox @change="toggleSelection(tableData)" style="margin-top:10px">全选</el-checkbox>
        <el-button style="margin:10px 10px;" @click="shanchu(tableData)">删除</el-button>
      </el-form>

    </div>
    <!-- 分页 -->
    <div class="pagniation">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="currentPage1" :page-size=this.size layout="total, prev, pager, next" :total="this.tableData.length" style="float:right;margin-right:150px;">
      </el-pagination>
    </div>
    <!-- 删除弹出框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span>这是一段内容确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getXmessageList, messageXdelList } from '../../api'
import lodash from 'lodash'
export default {
  data() {
    return {
      form: {
        name: '',
      },
      tableData: [],
      arr: [],
      row2: '',
      dialogVisible: false,
      // 分页
      currentPage1: 1,
      size: 5,
      tableData2: [],
      queryInfo: {
        query: '',
      },
    }
  },
  created() {
    getXmessageList().then((res) => {
      // console.log(res);
      this.tableData = res.data
      this.arr = res.data
      this.tableData2 = this.tableData.slice((this.currentPage1 - 1) * this.size, this.currentPage1 * this.size)
    })
  },
  methods: {
    // 封装
    mes() {
      getXmessageList().then((res) => {
        // console.log(res);
        this.tableData = res.data
        this.arr = res.data
        this.tableData2 = this.tableData.slice((this.currentPage1 - 1) * this.size, this.currentPage1 * this.size)
      })
    },
    // 全选
    toggleSelection(rows) {
      // this.$refs.multipleTable.clearSelection();
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
      console.log(rows)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(val)
    },
    setchecked(row) {
      row.checked = 1
    },
    //删除
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    deleteRow(row) {
      console.log(row)
      this.row2 = row
      // this.flag = true
      this.dialogVisible = true
    },
    sure() {
      messageXdelList(this.row2).then((res) => {
        console.log(res)
        this.mes()
      })

      this.dialogVisible = false
    },
    // 搜索
    getUserList() {
      let search = this.queryInfo.query
      this.tableData2 = this.tableData.filter((item) => {
        // total = this.tableData2.length
        console.log(this.size)
        return Object.keys(item).some(function (key) {
          return String(item[key]).toLowerCase().indexOf(search) > -1
        })
      })
    },
    // 删除提示
    cancel() {
      this.visible = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.size = val
      // this.fn()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage1 = val
      this.tableData2 = this.tableData.slice((this.currentPage1 - 1) * this.size, this.currentPage1 * this.size)
    },
    // 全选删除
    shanchu() {
      console.log(this.multipleSelection)
      this.multipleSelection.map((v) => {
        console.log(v)
        messageXdelList({ id: v.id }).then((res) => {
          console.log(res)
          this.mes()
        })
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.messageBox {
  width: 1000px;
  margin: 20px 30px;
}
.block {
  width: 80px;
  height: 30px;
}
</style>